﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Exhibition.Dao.ViewModel;
using AutoMapper;
namespace Exhibition.Dao{

   public class RoleFunDao     {


   public  RoleFunDao()   {
 
       Mapper.CreateMap<E_RoleFun, RoleFunModel>();
}
     public List<E_RoleFun> GetList(int startIndex, int maxRow, out int recordcount)
     {


       using(ExhibitionEntities edm=new ExhibitionEntities())
     {

         recordcount = edm.E_RoleFun.Count();

         return edm.E_RoleFun.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

         }

     }


   public RoleFunModel GetOneById(int id)

   {


       using( ExhibitionEntities edm=new ExhibitionEntities())
    {

      var query= edm.E_RoleFun.FirstOrDefault(s => s.Id == id);
     return  Mapper.Map<E_RoleFun, RoleFunModel>(query);
   }

   }


   public int Save(RoleFunModel obj)
   {


       using( ExhibitionEntities edm=new ExhibitionEntities())
   {

       var query = edm.E_RoleFun.FirstOrDefault(s => s.Id == obj.Id);

         if (query == null)

         {


          query = new E_RoleFun();

         edm.AddToE_RoleFun(query);

         }


          query.FunId=obj.FunId;

          query.RoleId=obj.RoleId;

       edm.SaveChanges();

       return query.Id;


   }

         }


   public int DelById(int id)
     {


       using( ExhibitionEntities edm=new ExhibitionEntities())
   {

       var query = edm.E_RoleFun.FirstOrDefault(s => s.Id == id);

          if (query != null)

            {


               edm.DeleteObject(query);


           }


         return edm.SaveChanges();


     }

     }


   }

 }

